<?php
function smarty_function_article_list($params, &$smarty)
{
	include($_ENV['webpath']."conn.php");
	include_once($_ENV['webpath']."model/article.php");
	
	$sqladd='';
	$art_posfix=$params['postfix']?$params['postfix']:'.html';
	$page=$params['page']?$params['page']:1;
	if($params['catalog_id']){
		$ids=get_all_sub_catalog($params['catalog_id']);
		$sqladd.=' and catalog in ('.$ids.')';
	}
	if($params['tag']){
		foreach(explode(',',$params['tag']) as $k=>$v){
			if($k==0){
				$sqladd.=" and locate('".$v."',label) ";
			}else{
				$sqladd.=" or locate('".$v."',label) ";
			}
		}
	}
	if($params['top']==1){
		$sqladd.=' and top=1';
	}elseif($params['top']===0){
		$sqladd.=' and top=0';
	}
	if($params['toutiao']==1){
		$sqladd.=' and toutiao=1';
	}elseif($params['toutiao']===0){
		$sqladd.=' and toutiao=0';
	}
	if($params['pic_only']==1){
		$sqladd.=" and pic != ''";
	}elseif($params['pic_only']===0){
		$sqladd.=" and pic = ''";
	}
	if($params['rec']==1){
		$sqladd.=' and rec=1';
	}elseif($params['rec']===0){
		$sqladd.=' and rec=0';
	}
	if($params['order']){
		$sqladd.=' order by '.$params['order'];
	}else{
		$sqladd.=' order by dt desc';
	}
	if(!empty($params['totalnums'])){
		if($total=mysql_fetch_array(mysql_query("select count(id) as c from ".$prefix."article where 1=1 ".$sqladd))){
			$totalnums=$total['c'];
		}
	}
	if($params['count']){
		$sqladd.=' limit '.($page-1)*$params['count'].",".$params['count'];
	}
	
    $sql="select * from ".$prefix."article where 1=1 ".$sqladd;
	$result=mysql_query($sql);
	while($row=mysql_fetch_array($result)){ 
		$str='/'.get_catalog_path($row['catalog']);  //例: /fushi/
		if($row['filename']){
			$str.=$row['filename'];                  //例: /fushi/myarticle.htm
		}else{
			$a=doquery($row['catalog']);
			$a=$a['folder'];
			$str.=$a.$row['id'].$art_posfix;            //例: /fushi/nvzhuang123.htm
		}
		$row['link']=$str;
		$row['content']=stripslashes($row['content']);
		$arr[]=$row;
	}
	
	if(!empty($params['totalnums'])){
		$smarty->assign($params['totalnums'],$totalnums);
	}
	if (empty($params['assign'])) {
		return $arr;
	} else {
		$smarty->assign($params['assign'],$arr);
	}
	mysql_close($conn);
}
?>
